import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers'
import { analyzer } from "vite-bundle-analyzer";

// https://vitejs.dev/config/
export default defineConfig(async ({ mode }) => ({
  plugins: [
    vue(),
    vueJsx(),
    AutoImport({
      imports: [
        'vue',
        // {
        //   'naive-ui': [
        //     'useDialog',
        //     'useMessage',
        //     'useNotification',
        //     'useLoadingBar'
        //   ]
        // }
      ],
      // resolvers: [NaiveUiResolver()]
    }),
    Components({
      extensions: ['vue', 'ts', 'tsx'],
      resolvers: [NaiveUiResolver()],
      dts: true,
      deep: true,
    }),
    // 分析打包插件
    // visualizer({
    //   // 打包完成后自动打开浏览器，显示产物体积报告
    //   open: true,
    // }),
    mode === "analyze" ? analyzer() : undefined,
  ],
  resolve: {
    alias: {
      "@": resolve(__dirname, 'src'), // 路径别名
    },
    extensions: ['.js', '.json', '.ts', '.tsx', '.vue'] // 使用路径别名时想要省略的后缀名，可以自己 增减
  },

  // Vite options tailored for Tauri development and only applied in `tauri dev` or `tauri build`
  //
  // 1. prevent vite from obscuring rust errors
  clearScreen: false,
  // 2. tauri expects a fixed port, fail if that port is not available
  server: {
    port: 1420,
    strictPort: true,
    watch: {
      // 3. tell vite to ignore watching `src-tauri`
      ignored: ["**/src-tauri/**"],
    },
    proxy: {
      '/api': 'http://10.101.37.217'
    }
  },
  build: {
    chunkSizeWarningLimit: 1024,
    // rollupOptions: {
    //   output:{
    //     // 拆包
    //     manualChunks(id: string) {
    //       if (id.includes('naive-ui')) {
    //         return 'naive-ui'
    //       } else {
    //         return 'main'
    //       }
    //     },
    //   }
    // }
  },
}));
